﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DBConstruct;
using System.Data;
/// <summary>
///DBAccessor 的摘要说明
/// </summary>
public class DBAccessor
{
    private static DatabaseFactory _DBFactory;
    /// <summary>
    /// DBFactory属性
    /// </summary>
    public DatabaseFactory DBFactory
    {
        get { return _DBFactory; }
        set { _DBFactory = value; }
    }

    private static DBConnStringBuilderFactory _DBConnStringBuilder;
    /// <summary>
    /// DBConnStringBuilder属性
    /// </summary>
    public DBConnStringBuilderFactory DBConnStringBuilder
    {
        get { return _DBConnStringBuilder; }
        set { _DBConnStringBuilder = value; }
    }

    public DBAccessor()
    {
    }

    /// <summary>
    /// 创建双工厂，由DataAccessor静态方法来管理
    /// </summary>
    /// <param name="connectionString">连接字符串</param>
    /// <param name="DBtype">DBMS类型</param>
    public void DBAccessDriver(string connectionString, string DBtype)
    {
        if (DBtype == DBType.AccessType)
        {
            _DBFactory = new AccessDatabase(connectionString);
            //
        }
        else if (DBtype == DBType.OracleType)
        {
            _DBFactory = new OracleDatabase();
            //
        }
        else if (DBtype == DBType.SQLType)
        {
            _DBFactory = new SQLDatabase(connectionString);
            _DBConnStringBuilder = new SQLConnStringBuilder(connectionString);
        }
        //else if{}
    }

    /// <summary>
    /// 创建数据库工厂
    /// </summary>
    /// <param name="connectionString">连接字符串</param>
    /// <param name="DBtype">DBMS类型</param>
    /// <returns>返回一个实例</returns>
    public DatabaseFactory DBFactoryDriver(string connectionString, string DBtype)
    {
        if (DBtype == DBType.AccessType)
        {
            return new AccessDatabase(connectionString);
        }
        else if (DBtype == DBType.OracleType)
        {
            return new OracleDatabase();
        }
        else if (DBtype == DBType.SQLType)
        {
            return new SQLDatabase(connectionString);
        }
        //else if(){}
        else return null;
    }

    /// <summary>
    /// 查询数据库中存在某一值(或元组)，有参数组
    /// </summary>
    /// <param name="cmd">命令</param>
    /// <param name="parameters">参数组</param>
    /// <returns>布尔值</returns>
    public static bool QueryExistParams(string cmd, object[] parameters)
    {
        return _DBFactory.IsExist(cmd, parameters);
    }

    /// <summary>
    /// 查询数据库中存在某一值(或元组)，无参数组
    /// </summary>
    /// <param name="cmd">命令</param>
    /// <returns>布尔值</returns>
    public static bool QueryExistNonParams(string cmd)
    {
        return _DBFactory.IsExist(cmd, null);
    }

    /// <summary>
    /// 执行查询，并返回数据集，有参数组
    /// </summary>
    /// <param name="cmd">命令</param>
    /// <param name="parameters">参数组</param>
    /// <returns>数据集</returns>
    public static DataSet QueryDataSetParams(string cmd, object[] parameters)
    {
        return _DBFactory.ExcuteReader(cmd, parameters);
    }

    /// <summary>
    /// 执行查询，并返回数据集，有参数组
    /// </summary>
    /// <param name="cmd">命令</param>
    /// <returns>数据集</returns>
    public static DataSet QueryDataSetNonParams(string cmd)
    {
        return _DBFactory.ExcuteReader(cmd, null);
    }

    /// <summary>
    /// 执行插入、更新、删除及存储过程等，返回影响的行数，有命令参数组
    /// </summary>
    /// <param name="cmd">命令</param>
    /// <param name="parameters">参数组</param>
    /// <returns>影响的行数</returns>
    public static int ReturnNonQueryParams(string cmd, object[] parameters)
    {
        return _DBFactory.ExcuteNonQuery(cmd, parameters);
    }

    /// <summary>
    /// 执行插入、更新、删除及存储过程等，返回影响的行数，无命令参数组
    /// </summary>
    /// <param name="cmd">命令</param>
    /// <returns>影响的行数</returns>
    public static int ReturnNonQueryParams(string cmd)
    {
        return _DBFactory.ExcuteNonQuery(cmd, null);
    }

}
